package com.geekchique.runningbuddy.activity;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.provider.ActivityID;

import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewInflate;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

/*
 * TabActivity is still currently marked deprecated, but according to this
 * post it should be unmarked:
 *
 *   http://groups.google.com/group/android-developers/browse_thread/thread/61976a6a2d86673d
 */
@SuppressWarnings("deprecation")
public class TabsAdapter extends TabActivity
{
	private Bundle extras;

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.tabs_adapter);

        TabHost tabs = getTabHost();
    	this.extras = getIntent().getExtras();

        for (int i = 0; i < (extras.size() -1) - ((extras.size()-1) * 0.5); i++) {
        	String currentNo = new String(Integer.toString(i));
			String name = (String)this.extras.get("name" + currentNo);
			String activity = (String) this.extras.get("tab" + currentNo);

        	ComponentName activityByReference =	new ComponentName(ActivityID.package_context, ActivityID.activities_package + activity);

        	TabHost.TabSpec tab = tabs.newTabSpec(activity);
        	tab.setContent(new Intent().setComponent(activityByReference));
        	tab.setIndicator(new MyTabIndicator(this, name));
        	tabs.addTab(tab);
		}

        tabs.setCurrentTab(this.extras.getInt("default"));

    }

    public static class MyTabIndicator extends LinearLayout {
		public MyTabIndicator(Context context, String label) {
			super(context);

			View tab = ViewInflate.from(context).inflate(R.layout.tab, this, null);

			TextView textView = (TextView)tab.findViewById(R.id.tab_label);
			textView.setText(label);
		}
    }
}